package shc.graduation.http.task;

import android.text.TextUtils;

import org.litepal.crud.DataSupport;

import java.util.List;

import shc.graduation.bean.CodeGroup;
import shc.graduation.bean.CodeInfo;
import shc.graduation.utils.ServerUtil;
import shc.graduation.utils.log.LogUtils;

/**
 * Created by shuhuiChen on  2016/5/21  14:35.
 * Email ：shuhuiCSH@126.com
 */
public class CodeInfoTask extends BaseTask<CodeInfo> {

    @Override
    void executeTask() {

        if (mBaseGetter == null)
            return;

        boolean isStop = false;  //是否暂停继续抓取网页内容
        int index = mTaskOption.pageIndex; //开始的索引

        while (!isStop) {

            String url = mTaskOption.websiteUrl + index;// 拼接请求网址链接

            ListenerRunning("正在请求第" + index + "页");

            mModelList = mBaseGetter.getModel(url);

            if (mModelList != null) {
                LogUtils.e("返回对象数量:" + mModelList.size());
            }

            if (mModelList != null && mModelList.size() > 0) {

                ListenerRunning("请求的网页内容有东西");

                // 在这里检查数据库是否已经存在了
                CodeGroup codeGroup = null;
                List<CodeInfo> codeInfoList = null;
                List<CodeGroup> codeGroupList = null;

                for (CodeInfo code : mModelList) {

                    codeInfoList = DataSupport.where("contenturl = ?", code.getContentUrl()).find(CodeInfo.class);

                    if (codeInfoList != null && codeInfoList.size() <= 0) {

                        ListenerRunning(code.getName() + "：数据库中木有");

                        //获取这个项目在Github中的地址
                        String github = getGithubUrl(ServerUtil.PAOWANG_URL + code.getContentUrl());
                        LogUtils.e("Github地址:" + github);
                        code.setGithubUrl(github);
                        code.save();

                        codeGroupList = DataSupport.where("groupname = ?", code.getGroupName()).find(CodeGroup.class);

                        if (codeGroupList != null && codeGroupList.size() <= 0) {
                            LogUtils.e("不存在该分组");

                            ListenerRunning(code.getGroupName() + "：不存在该分组");

                            codeGroup = new CodeGroup(code.getGroupName());
                            codeGroup.setCreateTime(String.valueOf(System.currentTimeMillis()));
                            codeGroup.save();

                        } else {
                            LogUtils.e("存在该分组");
                            codeGroup = codeGroupList.get(0);

                        }
                        code.setCodegroup_id(codeGroup.getId());
                        code.update(code.getId());

                        ListenerRunning(code.getName() + "：保存好了");
                        LogUtils.e("保存好了");
                    } else {
                        ListenerRunning(code.getName() + "：数据库中已存在");
                        isStop = true;
                        ListenerSuccess("成功");
                        LogUtils.e("数据库中已存在，停止循环");
                        break;//跳出这个循环
                    }
                }
            } else {
                isStop = true;
                ListenerSuccess("成功");
                LogUtils.e("数据为空，停止循环");
            }
            index++;
            if (mModelList != null) {
                mModelList.clear();
                mModelList = null;
            }
        }
    }

    private String getGithubUrl(String url) {

        String htmlDataString = mBaseGetter.mIHttpGet.getContent(url);

        String githubUrl = null;
        if (!TextUtils.isEmpty(htmlDataString) && htmlDataString.contains("var address")) {
            int index = htmlDataString.indexOf("var address");
            String subsString = htmlDataString.substring(index, index + 150);
            int firstIndex = subsString.indexOf("\"");
            int secondIndex = subsString.indexOf("\"", firstIndex + 1);
            githubUrl = subsString.substring(firstIndex + 1, secondIndex);
        }

        return githubUrl;
    }
}
